﻿using System;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;

namespace QuickStart.UnitTests
{
	[TestClass]
	public class EditCustomersPresenterFixture
	{
		[TestMethod]
		public void ShouldSetPresenterOnViewOnConstruct()
		{
			var repository = new MemoryCustomerRepository();
			var view = new MockView();
			var presenter = new EditCustomersPresenter(view, repository);

			Assert.AreSame(presenter, view.Presenter);
		}

		[TestMethod]
		public void ShouldSearchAndSetResults()
		{
			var repository = new MemoryCustomerRepository();
			var view = new MockView();
			var presenter = new EditCustomersPresenter(view, repository);

			repository.Create(new Customer { FirstName = "Joe", LastName = "Doe" });
			repository.Create(new Customer { FirstName = "Sarah", LastName = "Smith" });

			presenter.Search("Joe");

			Assert.IsNotNull(view.Results);
			Assert.IsTrue(view.Results.Any(c => c.FirstName == "Joe"));
			Assert.IsFalse(view.Results.Any(c => c.FirstName == "Sarah"));
		}

		[TestMethod]
		public void EmptySearchShowsAll()
		{
			var repository = new MemoryCustomerRepository();
			var view = new MockView();
			var presenter = new EditCustomersPresenter(view, repository);

			repository.Create(new Customer { FirstName = "Joe", LastName = "Doe" });
			repository.Create(new Customer { FirstName = "Sarah", LastName = "Smith" });

			presenter.Search("");

			Assert.IsNotNull(view.Results);
			Assert.AreEqual(2, view.Results.Count());
		}

		[TestMethod]
		public void NullSearchShowsAll()
		{
			var repository = new MemoryCustomerRepository();
			var view = new MockView();
			var presenter = new EditCustomersPresenter(view, repository);

			repository.Create(new Customer { FirstName = "Joe", LastName = "Doe" });
			repository.Create(new Customer { FirstName = "Sarah", LastName = "Smith" });

			presenter.Search(null);

			Assert.IsNotNull(view.Results);
			Assert.AreEqual(2, view.Results.Count());
		}

		[TestMethod]
		public void SaveSetsStatus()
		{
			var repository = new MemoryCustomerRepository();
			var view = new MockView();
			var presenter = new EditCustomersPresenter(view, repository);

			repository.Create(new Customer { FirstName = "Joe", LastName = "Doe" });
			var id = repository.Create(new Customer { FirstName = "Sarah", LastName = "Smith" });

			presenter.Save(new Customer { Id = id, FirstName = "Sarah", LastName = "Brown" });

			Assert.IsNotNull(view.Status);
		}

		public class MockView : IEditCustomersView
		{
			public string Status { get; private set; }
			public IEnumerable<Customer> Results { get; private set; }

			public EditCustomersPresenter Presenter { get; set; }

			public System.Windows.Forms.DialogResult ShowDialog()
			{
				return System.Windows.Forms.DialogResult.OK;
			}

			public void SetStatus(string status)
			{
				this.Status = status;
			}

			public void SetResults(IEnumerable<Customer> searchResults)
			{
				this.Results = searchResults;
			}
		}
	}
}
